package com.example.elmmerchants.Create_Shop;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Set_Shop_Data extends AppCompatActivity {

    RelativeLayout shop_information, qualification_information, legal_information;
    TextView shop_information_status, qualification_information_status, legal_information_status;

    String user_email, first_category, second_category, shop_status, qualification_status, legal_status;
    boolean is_loading_qualification_page = false, is_loading_all_data = false;
    String location, licence_status, food_licence_status;
    AlertDialog dialog;

    private class Loading_Qualification_Data_Thread extends Thread {
        @Override
        public void run() {
            is_loading_qualification_page = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL connectionSql = new Connection_SQL();
                    Connection connection = connectionSql.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, user_email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        if (set.getString("shop_place").isEmpty()) {
                            location = "暂无";
                        }
                        else {
                            location = set.getString("shop_place");
                        }

                        if (set.getBlob("licence_picture") == null) {
                            licence_status = "去上传";
                        }
                        else {
                            licence_status = "已上传";
                        }

                        if (set.getBlob("food_business_licence_picture") == null) {
                            food_licence_status = "去上传";
                        }
                        else {
                            food_licence_status = "已上传";
                        }
                        is_loading_qualification_page = true;
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_qualification_page) {
                try {
                    Thread.sleep( 1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    System.out.println(licence_status);
                    Intent intent = new Intent(Set_Shop_Data.this, Qualification_Information_Page.class);
                    intent.putExtra("user_email", user_email);
                    intent.putExtra("shop_information_status", shop_information_status.getText().toString());
                    intent.putExtra("qualification_information_status", qualification_information_status.getText().toString());
                    intent.putExtra("legal_information_status", legal_information_status.getText().toString());
                    intent.putExtra("location", location);
                    intent.putExtra("licence_status", licence_status);
                    intent.putExtra("food_licence_status", food_licence_status);
                    startActivityForResult(intent, 1);
                }
            });
        }
    }

    private class Loading_All_Data_Thread extends Thread {
        @Override
        public void run() {
            is_loading_all_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, user_email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        if (set.getString("email").equals(user_email)) {
                            first_category = set.getString("first_category");
                            second_category = set.getString("second_category");

                            if (set.getInt("shop_information") == 1) {
                                shop_status = "已录入";
                            }
                            else {
                                shop_status = "待录入";
                            }

                            if (set.getInt("qualification_information") == 1) {
                                qualification_status = "已录入";
                            }
                            else {
                                qualification_status = "待录入";
                            }

                            if (set.getInt("legal_information") == 1) {
                                legal_status = "已录入";
                            }
                            else {
                                legal_status = "待录入";
                            }
                        }
                    }

                    is_loading_all_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }

                while (!is_loading_all_data) {
                    try {
                        Thread.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                        if (shop_status.equals("已录入")) {
                            shop_information_status.setText("已录入");
                        }
                        else {
                            shop_information_status.setText("待录入");
                        }

                        if (qualification_status.equals("已录入")) {
                            qualification_information_status.setText("已录入");
                        }
                        else {
                            qualification_information_status.setText("待录入");
                        }

                        if (legal_status.equals("已录入")) {
                            legal_information_status.setText("已录入");
                        }
                        else {
                            legal_information_status.setText("待录入");
                        }

                        if (shop_status.equals("已录入") && qualification_status.equals("已录入") && legal_status.equals("已录入")) {
                            startActivity(new Intent(Set_Shop_Data.this, Approved_Page.class));
                            finish();
                        }

                        shop_information.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (shop_status.equals("待录入")) {
                                    Intent intent = new Intent(Set_Shop_Data.this, Shop_Information_Page.class);
                                    intent.putExtra("user_email", user_email);
                                    intent.putExtra("first_category", first_category);
                                    intent.putExtra("second_category", second_category);
                                    intent.putExtra("shop_information_status", shop_information_status.getText().toString());
                                    intent.putExtra("qualification_information_status", qualification_information_status.getText().toString());
                                    intent.putExtra("legal_information_status", legal_information_status.getText().toString());
                                    startActivityForResult(intent, 1);
                                }
                            }
                        });

                        qualification_information.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (qualification_status.equals("待录入")) {
                                    set_dialog();
                                    Loading_Qualification_Data_Thread loadingQualificationDataThread = new Loading_Qualification_Data_Thread();
                                    loadingQualificationDataThread.start();
                                }
                            }
                        });

                        legal_information.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (legal_status.equals("待录入")) {
                                    Intent intent = new Intent(Set_Shop_Data.this, Legal_Information_Page.class);
                                    intent.putExtra("user_email", user_email);
                                    intent.putExtra("shop_information_status", shop_information_status.getText().toString());
                                    intent.putExtra("qualification_information_status", qualification_information_status.getText().toString());
                                    intent.putExtra("legal_information_status", legal_information_status.getText().toString());
                                    startActivityForResult(intent, 1);
                                }
                            }
                        });
                    }
                });
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_shop_data);

        user_email = getIntent().getStringExtra("user_email");
        first_category = getIntent().getStringExtra("first_category");
        second_category = getIntent().getStringExtra("second_category");
        shop_status = getIntent().getStringExtra("shop_information_status");
        qualification_status = getIntent().getStringExtra("qualification_information_status");
        legal_status = getIntent().getStringExtra("legal_information_status");

        shop_information =  findViewById(R.id.shop_information);
        shop_information_status = findViewById(R.id.shop_information_status);
        qualification_information_status = findViewById(R.id.qualification_information_status);
        legal_information_status = findViewById(R.id.legal_information_status);
        qualification_information = findViewById(R.id.qualification_information);
        legal_information = findViewById(R.id.legal_information);

        if (shop_status.equals("已录入")) {
            shop_information_status.setText("已录入");
        }
        else {
            shop_information_status.setText("待录入");
        }

        if (qualification_status.equals("已录入")) {
            qualification_information_status.setText("已录入");
        }
        else {
            qualification_information_status.setText("待录入");
        }

        if (legal_status.equals("已录入")) {
            legal_information_status.setText("已录入");
        }
        else {
            legal_information_status.setText("待录入");
        }

        if (shop_status.equals("已录入") && qualification_status.equals("已录入") && legal_status.equals("已录入")) {
            startActivity(new Intent(Set_Shop_Data.this, Approved_Page.class));
            finish();
        }

        shop_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (shop_status.equals("待录入")) {
                    Intent intent = new Intent(Set_Shop_Data.this, Shop_Information_Page.class);
                    intent.putExtra("user_email", user_email);
                    intent.putExtra("first_category", first_category);
                    intent.putExtra("second_category", second_category);
                    intent.putExtra("shop_information_status", shop_information_status.getText().toString());
                    intent.putExtra("qualification_information_status", qualification_information_status.getText().toString());
                    intent.putExtra("legal_information_status", legal_information_status.getText().toString());
                    startActivityForResult(intent, 1);
                }
            }
        });

        qualification_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (qualification_status.equals("待录入")) {
                    set_dialog();
                    Loading_Qualification_Data_Thread loadingQualificationDataThread = new Loading_Qualification_Data_Thread();
                    loadingQualificationDataThread.start();
                }
            }
        });

        legal_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (legal_status.equals("待录入")) {
                    Intent intent = new Intent(Set_Shop_Data.this, Legal_Information_Page.class);
                    intent.putExtra("user_email", user_email);
                    intent.putExtra("shop_information_status", shop_information_status.getText().toString());
                    intent.putExtra("qualification_information_status", qualification_information_status.getText().toString());
                    intent.putExtra("legal_information_status", legal_information_status.getText().toString());
                    startActivityForResult(intent, 1);
                }
            }
        });
    }

    void get_status() {
        set_dialog();
        Loading_All_Data_Thread loadingAllDataThread = new Loading_All_Data_Thread();
        loadingAllDataThread.start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:{
                get_status();
                break;
            }
        }
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(Set_Shop_Data.this);
        LayoutInflater inflater1 = LayoutInflater.from(Set_Shop_Data.this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}